
import Quadtree from "@timohausmann/quadtree-js";
import { Main } from "../Main";
import { CollisionObj } from "./CollisionObj";

export class CollisonManager {
	left=0
	right=0
	top=0
	bottom=0

	quadTree:Quadtree
    constructor(){
		var w=Main.I.screenWidth+300
		var h=Main.I.screenHeight+300
		this.left=-w/2
		this.top=-h/2
		this.right=w/2
		this.bottom=h/2
		this.quadTree=new Quadtree({
			x:-w/2,
			y:-h/2,
			width:w,
			height:h
		})
	}

	reset(){
		this.quadTree.clear()
	}
	insert(collisionObj:CollisionObj){
		if(collisionObj.centerX<this.left || collisionObj.centerX>this.right || collisionObj.centerY<this.top || collisionObj.centerY>this.bottom){
			return
		}
		this.quadTree.insert(collisionObj)
	}
	/**
	 * 将碰撞的物体排开
	 */
	repulse(){
		
	}


	testIntersect(a:CollisionObj,b:CollisionObj){
		return Math.abs(b.centerY-a.centerY)+Math.abs(b.centerX-a.centerX) < (a.width+b.width+a.height+b.height)/2
	}
}

